/*******************************************************************************

This code file performs a supplementary analysis for a reviewer to illustrate
robustness to excluding 2006-2008 or 2007-2009 from the sample.

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"
	
	local n_boot = 200

*** Run logits

	use "$data/clean/cleaned_data.dta", clear

	* Baseline
	logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta)
	
	* Exclude 2006-2008
	logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit if !inlist(yearpermit,2006,2007,2008), cl(nta)
	
	* Exlude 2007-2009
	logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit if !inlist(yearpermit,2007,2008,2009), cl(nta)

*** Modify bootstrap programs

	xtset,clear

	quietly {
	capture program drop boot_mcost
	program define boot_mcost, rclass
	
		syntax [, Year(real 2006)]
				 		  
		preserve
		
		if `year' == 2006 {
			logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit if !inlist(yearpermit,2006,2007,2008), cl(nta_boot) asis
		}
		if `year' == 2007 {
			logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit if !inlist(yearpermit,2007,2008,2009), cl(nta_boot) asis
		}
		
		mat b = e(b)
		local coef_dtaxrate = b[1,1]
		
		predict xb, xb
		
		gen pr = exp(xb)/(1+exp(xb))
		
		gen pr_plus = exp(xb+`coef_dtaxrate'*0.001)/(1+exp(xb+`coef_dtaxrate'*0.001))
		
		gen mvalue_bldg = (assesstot - assessland)/(frac_assess*underassess)
			
		gen assessprop = pr * mvalue_bldg * dtaxrate_onsite
		gen assessprop_plus = pr_plus * mvalue_bldg * (dtaxrate_onsite + 0.001)
		
		local sh_inclusionary = 0.20
		gen inclusionaryunits = pr * floor(`sh_inclusionary' * unitsres)
		gen inclusionaryunits_plus = pr_plus * floor(`sh_inclusionary' * unitsres)
		
		collapse (sum) assessprop assessprop_plus inclusionaryunits inclusionaryunits_plus
		
		gen mcostperunit = (assessprop_plus-assessprop)/(inclusionaryunits_plus-inclusionaryunits)
		
		summ mcostperunit
		return scalar tmp = r(mean)
		
		restore
		
	end
	
	capture program drop boot_elasticity
	program define boot_elasticity, rclass
	
		syntax [, Year(real 2006)]
				 		  
		preserve
						
		if `year' == 2006 {
			logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit if !inlist(yearpermit,2006,2007,2008), cl(nta_boot) asis
		}
		if `year' == 2007 {
			logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit if !inlist(yearpermit,2007,2008,2009), cl(nta_boot) asis
		}
		mat b = e(b)
		local coef_dtaxrate = b[1,1]
		
		predict xb, xb
			
		gen pr = exp(xb)/(1+exp(xb))
		gen pr_plus = exp(xb+`coef_dtaxrate'*0.001)/(1+exp(xb+`coef_dtaxrate'*0.001))
	
		collapse (mean) pr_plus pr [aw=unitsres]
	
		gen elast = 1000*(pr_plus - pr)
		
		summ elast
		return scalar tmp = r(mean)
		
		restore
		
	end
	
	
	capture program drop boot_acost
	program define boot_acost, rclass
	
		syntax [, Year(real 2006)]
				 		  
		preserve
								
		if `year' == 2006 {
			logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit if !inlist(yearpermit,2006,2007,2008), cl(nta_boot) asis
		}
		if `year' == 2007 {
			logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit if !inlist(yearpermit,2007,2008,2009), cl(nta_boot) asis
		}
		mat b = e(b)
		local coef_dtaxrate = b[1,1]
		
		predict xb, xb
		
		gen pr = exp(xb)/(1+exp(xb))
				
		gen mvalue_bldg = (assesstot - assessland)/(frac_assess*underassess)
			
		gen assessprop = pr * mvalue_bldg * dtaxrate_onsite
		
		local sh_inclusionary = 0.20
		gen inclusionaryunits = pr * floor(`sh_inclusionary' * unitsres)
		
		collapse (sum) assessprop inclusionaryunits
		
		gen acostperunit = assessprop/inclusionaryunits
		
		summ acostperunit
		return scalar tmp = r(mean)
		
		restore
		
	end
	}
	
*** Run bootstrap programs

	* 2006-2008
	bootstrap tmp=r(tmp), seed(12345) reps(`n_boot') cluster(nta) idcluster(nta_boot): boot_mcost, y(2006)
	bootstrap tmp=r(tmp), seed(12345) reps(`n_boot') cluster(nta) idcluster(nta_boot): boot_acost, y(2006)
	bootstrap tmp=r(tmp), seed(12345) reps(`n_boot') cluster(nta) idcluster(nta_boot): boot_elasticity, y(2006)
	
	* 2007-2009
	local n_boot = 200
	bootstrap tmp=r(tmp), seed(12345) reps(`n_boot') cluster(nta) idcluster(nta_boot): boot_mcost, y(2007)
	bootstrap tmp=r(tmp), seed(12345) reps(`n_boot') cluster(nta) idcluster(nta_boot): boot_acost, y(2007)
	bootstrap tmp=r(tmp), seed(12345) reps(`n_boot') cluster(nta) idcluster(nta_boot): boot_elasticity, y(2007)
	
*** Reset bootstrap programs

	do "$code/modules/bootstrap_programs.do"
